#include "StdAfx.h"
#include "AltiumCircuitHarnessConnector.h"

CAltiumCircuitSheetFileName::CAltiumCircuitSheetFileName( void )
{
	m_bHide = false;
}

CAltiumCircuitSheetFileName::CAltiumCircuitSheetFileName( const CAltiumCircuitSheetFileName* pEntityObj )
	:CCircuitAnnotation(pEntityObj)

{
	m_bHide = pEntityObj->IsHidden();
}

CAltiumCircuitSheetFileName::CAltiumCircuitSheetFileName( CICircuit* pICircuit )
	:CCircuitAnnotation(pICircuit)
{
	m_bHide = false;
}


CAltiumCircuitSheetFileName::~CAltiumCircuitSheetFileName( void )
{

}

void CAltiumCircuitSheetFileName::Serialize( CArchive & ar )
{
	if (ar.IsStoring())
	{
		ar << m_bHide;
	}
	else
	{
		ar >> m_bHide;
	}
	CCircuitAnnotation::Serialize(ar);
}

CObj * CAltiumCircuitSheetFileName::Clone() const
{
	return new CAltiumCircuitSheetFileName(this);
}

CDisplayObj * CAltiumCircuitSheetFileName::CreateDisplayObj( long lLayerId )
{
	CCircuitAnnotationDisObj* pDisObj = new CCircuitAnnotationDisObj(this);
	pDisObj->SetLayerId(lLayerId);
	return pDisObj;
}

void CAltiumCircuitSheetFileName::CalcBox()
{
	CCircuitAnnotation::CalcBox();
}

eCircuitUnitEntType CAltiumCircuitSheetFileName::GetUnitType() const
{
	return ALTIUM_CIRCUIT_SHEET_FILENAME;
}

//////////////////////////////////////////////////////////////////////////
CAltiumCircuitSheetDesignator::CAltiumCircuitSheetDesignator( void )
{
	m_bHide = false;
}

CAltiumCircuitSheetDesignator::CAltiumCircuitSheetDesignator( const CAltiumCircuitSheetDesignator* pEntityObj )
	:CCircuitAnnotation(pEntityObj)
{
	m_bHide = pEntityObj->IsHidden();
}

CAltiumCircuitSheetDesignator::CAltiumCircuitSheetDesignator( CICircuit* pICircuit )
	:CCircuitAnnotation(pICircuit)
{
	m_bHide = false;
}

CAltiumCircuitSheetDesignator::~CAltiumCircuitSheetDesignator( void )
{

}

void CAltiumCircuitSheetDesignator::Serialize( CArchive & ar )
{
	if (ar.IsStoring())
	{
		ar << m_bHide;
	}
	else
	{
		ar >> m_bHide;
	}
	CCircuitAnnotation::Serialize(ar);
}

CObj * CAltiumCircuitSheetDesignator::Clone() const
{
	return new CAltiumCircuitSheetDesignator(this);
}

CDisplayObj * CAltiumCircuitSheetDesignator::CreateDisplayObj( long lLayerId )
{
	CCircuitAnnotationDisObj* pDisObj = new CCircuitAnnotationDisObj(this);
	pDisObj->SetLayerId(lLayerId);
	return pDisObj;
}

void CAltiumCircuitSheetDesignator::CalcBox()
{
	CCircuitAnnotation::CalcBox();
}

eCircuitUnitEntType CAltiumCircuitSheetDesignator::GetUnitType() const
{
	return ALTIUM_CIRCUIT_SHEET_DESIGNATOR;
}

//////////////////////////////////////////////////////////////////////////
CAltiumCircuitSheetSymbol::CAltiumCircuitSheetSymbol(void)
{
	m_dblPosX = 0.0;

	m_dblPosY = 0.0;

	m_dblXSize = 0.0;

	m_dblYSize = 0.0;

	m_bFilled = false;

	m_dblBorderWidth = 0.0;
	m_dblBorderColor[0] = 0.0;
	m_dblBorderColor[1] = 0.0;
	m_dblBorderColor[2] = 0.0;

	m_dblFillColor[0] = 0.0;
	m_dblFillColor[1] = 0.0;
	m_dblFillColor[2] = 0.0;

	m_szSymbolType =_T("");
	m_sheetFileName = NULL;

	m_sheetDesignator = NULL; 
}

CAltiumCircuitSheetSymbol::CAltiumCircuitSheetSymbol( const CAltiumCircuitSheetSymbol* pEntityObj )
	:CCircuitUnit(pEntityObj)
{
	m_dblPosX = pEntityObj->GetPosX();

	m_dblPosY = pEntityObj->GetPosY();

	m_dblXSize = pEntityObj->GetXSize();

	m_dblYSize = pEntityObj->GetYSize();

	m_bFilled = pEntityObj->IsFilled();

	m_dblBorderWidth = pEntityObj->GetBorderWidth();

	m_dblBorderColor[0] = pEntityObj->GetBorderColorR();
	m_dblBorderColor[1] = pEntityObj->GetBorderColorG();
	m_dblBorderColor[2] = pEntityObj->GetBorderColorB();

	m_dblFillColor[0] = pEntityObj->GetFillColorR();
	m_dblFillColor[1] = pEntityObj->GetFillColorG();
	m_dblFillColor[2] = pEntityObj->GetFillColorB();

	m_szSymbolType = pEntityObj->GetSymbolType();
	m_sheetFileName = pEntityObj->GetSheetFileName();

	m_sheetDesignator = pEntityObj->GetSheetDesignator();
	m_vecSheetEntry = pEntityObj->GetSheetEntryList();
}

CAltiumCircuitSheetSymbol::CAltiumCircuitSheetSymbol( CICircuit* pICircuit )
	:CCircuitUnit(pICircuit)
{
	m_dblPosX = 0.0;

	m_dblPosY = 0.0;

	m_dblXSize = 0.0;

	m_dblYSize = 0.0;

	m_bFilled = false;

	m_dblBorderWidth = 0.0;

	m_dblBorderColor[0] = 0.0;
	m_dblBorderColor[1] = 0.0;
	m_dblBorderColor[2] = 0.0;

	m_dblFillColor[0] = 0.0;
	m_dblFillColor[1] = 0.0;
	m_dblFillColor[2] = 0.0;

	m_szSymbolType =_T("");

	m_sheetFileName = NULL;

	m_sheetDesignator = NULL; 
}


CAltiumCircuitSheetSymbol::~CAltiumCircuitSheetSymbol(void)
{
}



void CAltiumCircuitSheetSymbol::Serialize( CArchive & ar )
{
	if (ar.IsStoring())
	{
		ar<<m_dblPosX;
		ar<<m_dblPosY;
		ar<<m_dblXSize;
		ar<<m_dblYSize;
		ar<<m_bFilled;
		ar<<m_dblBorderWidth;
		ar<<m_dblBorderColor[0];
		ar<<m_dblBorderColor[1];
		ar<<m_dblBorderColor[2];
		ar<<m_dblFillColor[0];
		ar<<m_dblFillColor[1];
		ar<<m_dblFillColor[2];
		CObj::WriteString(ar,m_szSymbolType);
		m_sheetFileName->Serialize(ar);
		m_sheetDesignator->Serialize(ar);
		int nSize = m_vecSheetEntry.size();
		ar << nSize;
		for (int i =0 ;i<nSize;i++)
		{
			m_vecSheetEntry[i]->Serialize(ar);
		}

	}
	else
	{
		ar>>m_dblPosX;
		ar>>m_dblPosY;
		ar>>m_dblXSize;
		ar>>m_dblYSize;
		ar>>m_bFilled;
		ar>>m_dblBorderWidth;
		ar>>m_dblBorderColor[0];
		ar>>m_dblBorderColor[1];
		ar>>m_dblBorderColor[2];
		ar>>m_dblFillColor[0];
		ar>>m_dblFillColor[1];
		ar>>m_dblFillColor[2];
		CObj::ReadString(ar,m_szSymbolType);
		CAltiumCircuitSheetFileName* pSheetFileName = new CAltiumCircuitSheetFileName();
		pSheetFileName->Serialize(ar);
		SetSheetFileName(pSheetFileName);
		CAltiumCircuitSheetDesignator* pSheetDesignator = new CAltiumCircuitSheetDesignator();
		pSheetDesignator->Serialize(ar);
		SetSheetDesignator(pSheetDesignator);
		int nSize(0);
		ar >> nSize;
		for (int i =0 ;i<nSize;i++)
		{
			CAltiumCircuitSheetEntry* pEntry = new CAltiumCircuitSheetEntry();
			pEntry->Serialize(ar);
			AddSheetEntry(pEntry);
		}
	}
	CCircuitUnit::Serialize(ar);
}

CObj * CAltiumCircuitSheetSymbol::Clone() const
{
	return new CAltiumCircuitSheetSymbol(this);
}

void CAltiumCircuitSheetSymbol::CopyDisplayObj( long lSourceLayerId, long lDestLayerId )
{
	CCircuitUnit::CopyDisplayObj(lSourceLayerId,lDestLayerId);

	if (m_sheetFileName)
	{
		m_sheetFileName->CopyDisplayObj(lSourceLayerId,lDestLayerId);
	}
	if (m_sheetDesignator)
	{
		m_sheetDesignator->CopyDisplayObj(lSourceLayerId,lDestLayerId);
	}
	for (size_t i = 0; i<m_vecSheetEntry.size();i++)
	{
		m_vecSheetEntry[i]->CopyDisplayObj(lSourceLayerId,lDestLayerId);
	}

}

void CAltiumCircuitSheetSymbol::DeleteDisplayObj( long lLayerId )
{
	CCircuitUnit::DeleteDisplayObj(lLayerId);

	if (m_sheetFileName)
	{
		m_sheetFileName->DeleteDisplayObj(lLayerId);
	}
	if (m_sheetDesignator)
	{
		m_sheetDesignator->DeleteDisplayObj(lLayerId);
	}
	for (size_t i = 0; i<m_vecSheetEntry.size();i++)
	{
		m_vecSheetEntry[i]->DeleteDisplayObj(lLayerId);
	}

}

CDisplayObj * CAltiumCircuitSheetSymbol::CreateDisplayObj( long lLayerId )
{
	CAltiumCircuitSheetSymbolDisObj* pDisObj = new CAltiumCircuitSheetSymbolDisObj(this);
	pDisObj->SetLayerId(lLayerId);

	if (m_sheetFileName)
	{
		m_sheetFileName->CreateDisplayObj(lLayerId);
	}
	if (m_sheetDesignator)
	{
	    m_sheetDesignator->CreateDisplayObj(lLayerId);
	}
	for (size_t i = 0; i<m_vecSheetEntry.size();i++)
	{
		m_vecSheetEntry[i]->CreateDisplayObj(lLayerId);
	}

	return pDisObj;
}

eCircuitUnitEntType CAltiumCircuitSheetSymbol::GetUnitType() const
{
	return ALTIUM_CIRCUIT_SHEET_SYMBOL;
}


double CAltiumCircuitSheetSymbol::GetFillColorR() const
{
	return m_dblFillColor[0];
}

double CAltiumCircuitSheetSymbol::GetFillColorG() const
{
	return m_dblFillColor[1];
}

double CAltiumCircuitSheetSymbol::GetFillColorB() const
{
	return m_dblFillColor[2];
}

void CAltiumCircuitSheetSymbol::SetFillColorR(double colR)
{
	m_dblFillColor[0] = colR;
}

void CAltiumCircuitSheetSymbol::SetFillColorG(double colG)
{
	m_dblFillColor[1] = colG;
}

void CAltiumCircuitSheetSymbol::SetFillColorB(double colB)
{
	m_dblFillColor[2] = colB;
}

double CAltiumCircuitSheetSymbol::GetBorderColorR() const
{
	return m_dblBorderColor[0];
}

double CAltiumCircuitSheetSymbol::GetBorderColorG() const
{
	return m_dblBorderColor[1];
}

double CAltiumCircuitSheetSymbol::GetBorderColorB() const
{
	return m_dblBorderColor[2];
}

void CAltiumCircuitSheetSymbol::SetBorderColorR(double colR)
{
	m_dblBorderColor[0] = colR;
}

void CAltiumCircuitSheetSymbol::SetBorderColorG(double colG)
{
	m_dblBorderColor[1] = colG;
}

void CAltiumCircuitSheetSymbol::SetBorderColorB(double colB)
{
	m_dblBorderColor[2] = colB;
}

void CAltiumCircuitSheetSymbol::SetSheetFileName( CAltiumCircuitSheetFileName* pSheetFileName )
{
	m_sheetFileName = pSheetFileName;
	pSheetFileName->SetParentEntity(this);
}

void CAltiumCircuitSheetSymbol::SetSheetDesignator( CAltiumCircuitSheetDesignator* pSheetDesignator )
{
	m_sheetDesignator = pSheetDesignator;
	pSheetDesignator->SetParentEntity(this);
}

void CAltiumCircuitSheetSymbol::AddSheetEntry( CAltiumCircuitSheetEntry* sheetEntry )
{
	m_vecSheetEntry.push_back(sheetEntry);
	sheetEntry->SetParentEntity(this);
}

void CAltiumCircuitSheetSymbol::CalcBox()
{
	m_minX = m_dblPosX;
	m_minY = m_dblPosY - m_dblYSize;
	m_maxX = m_dblPosX + m_dblXSize;
	m_maxY = m_dblPosY;

	//sheet filename
	if (m_sheetFileName&&!m_sheetFileName->IsHidden())
	{
		m_sheetFileName->CalcBox();
		m_minX = min(m_minX, m_sheetFileName->GetBoxMinX());
		m_minY = min(m_minY, m_sheetFileName->GetBoxMinY());
		m_maxX = max(m_maxX, m_sheetFileName->GetBoxMaxX());
		m_maxY = max(m_maxY, m_sheetFileName->GetBoxMaxY());
	}

	//sheet designator
	if (m_sheetDesignator&&!m_sheetDesignator->IsHidden())
	{
		m_sheetDesignator->CalcBox();
		m_minX = min(m_minX, m_sheetDesignator->GetBoxMinX());
		m_minY = min(m_minY, m_sheetDesignator->GetBoxMinY());
		m_maxX = max(m_maxX, m_sheetDesignator->GetBoxMaxX());
		m_maxY = max(m_maxY, m_sheetDesignator->GetBoxMaxY());
	}

}



